library(gridExtra)
library(ggplot2)
library(foreign)

rm(list = ls())

# Set working directory. Please set your own
setwd("~/Desktop/PSRM Replication")

theme_base1 <- 
  theme_minimal(base_size=14)  + 
  theme(legend.position=c(0.15, .9),  legend.key.size = unit(1,"line"),
        axis.text=element_text(size=14),axis.title.x=element_text(size=14),axis.title.y=element_text(size=14),
        plot.title = element_text(size=14, hjust= 0.5))


data1 <- read.dta("Data/samealltogether15oneout.dta")
data<-data1[data1$id2==3 , ]



data1958<-data[data$id1==1958, ]
data1962<-data[data$id1==1962, ]
data1966<-data[data$id1==1966, ]
data1970<-data[data$id1==1970, ]
data1974<-data[data$id1==1974, ]
data1978<-data[data$id1==1978, ]
data1982<-data[data$id1==1982, ]
data1986<-data[data$id1==1986, ]
data1990<-data[data$id1==1990, ]
data1994<-data[data$id1==1994, ]
data1998<-data[data$id1==1998, ]
data2002<-data[data$id1==2002, ]
data2006<-data[data$id1==2006, ]
data2018<-data[data$id1==2018, ]




d1958<-ggplot(data1958, aes(x=id3, y =estimate)) +   
  geom_point(size = 1) +
  geom_errorbar(aes(ymax = max95, ymin = min95),width=0,  size=.4)+
  geom_errorbar(aes(ymax = max90, ymin = min90),width=0, size=.9)+
  scale_y_continuous(limits = c(-.1, .16))+ 
  scale_x_continuous(name="Election Counter",breaks = c(2,3,4,5,6,7,8,9,10,11,12,13,14,15),limits=c(1.5,15.5))+
  geom_hline(yintercept = 0, 
             linetype = 2, color = "black")+ ylab("ITT of Same-Election Eligibles") + xlab("")+
  theme_base1 + ggtitle("Without 1958") 
d1958

d1962<-ggplot(data1962, aes(x=id3, y =estimate)) +   
  geom_point(size = 1) +
  geom_errorbar(aes(ymax = max95, ymin = min95),width=0,  size=.4)+
  geom_errorbar(aes(ymax = max90, ymin = min90),width=0, size=.9)+
  scale_y_continuous(limits = c(-.1, .16))+ 
  scale_x_continuous(name="Election Counter",breaks = c(2,3,4,5,6,7,8,9,10,11,12,13,14,15),limits=c(1.5,15.5))+
  geom_hline(yintercept = 0, 
             linetype = 2, color = "black")+ ylab("ITT of Same-Election Eligibles") + xlab("")+
  theme_base1 + ggtitle("Without 1962") 
d1962

d1966<-ggplot(data1966, aes(x=id3, y =estimate)) +   
  geom_point(size = 1) +
  geom_errorbar(aes(ymax = max95, ymin = min95),width=0,  size=.4)+
  geom_errorbar(aes(ymax = max90, ymin = min90),width=0, size=.9)+
  scale_y_continuous(limits = c(-.1, .16))+ 
  scale_x_continuous(name="Election Counter",breaks = c(2,3,4,5,6,7,8,9,10,11,12,13,14,15),limits=c(1.5,15.5))+
  geom_hline(yintercept = 0, 
             linetype = 2, color = "black")+ ylab("ITT of Same-Election Eligibles") + xlab("")+
  theme_base1 + ggtitle("Without 1966") 
d1966

d1970<-ggplot(data1970, aes(x=id3, y =estimate)) +   
  geom_point(size = 1) +
  geom_errorbar(aes(ymax = max95, ymin = min95),width=0,  size=.4)+
  geom_errorbar(aes(ymax = max90, ymin = min90),width=0, size=.9)+
  scale_y_continuous(limits = c(-.1, .16))+ 
  scale_x_continuous(name="Election Counter",breaks = c(2,3,4,5,6,7,8,9,10,11,12,13,14,15),limits=c(1.5,15.5))+
  geom_hline(yintercept = 0, 
             linetype = 2, color = "black")+ ylab("ITT of Same-Election Eligibles") + xlab("")+
  theme_base1 + ggtitle("Without 1970") 
d1970

d1974<-ggplot(data1974, aes(x=id3, y =estimate)) +   
  geom_point(size = 1) +
  geom_errorbar(aes(ymax = max95, ymin = min95),width=0,  size=.4)+
  geom_errorbar(aes(ymax = max90, ymin = min90),width=0, size=.9)+
  scale_y_continuous(limits = c(-.1, .16))+ 
  scale_x_continuous(name="Election Counter",breaks = c(2,3,4,5,6,7,8,9,10,11,12,13,14,15),limits=c(1.5,15.5))+
  geom_hline(yintercept = 0, 
             linetype = 2, color = "black")+ ylab("ITT of Same-Election Eligibles") + xlab("")+
  theme_base1 + ggtitle("Without 1974") 
d1974


d1978<-ggplot(data1978, aes(x=id3, y =estimate)) +   
  geom_point(size = 1) +
  geom_errorbar(aes(ymax = max95, ymin = min95),width=0,  size=.4)+
  geom_errorbar(aes(ymax = max90, ymin = min90),width=0, size=.9)+
  scale_y_continuous(limits = c(-.1, .16))+ 
  scale_x_continuous(name="Election Counter",breaks = c(2,3,4,5,6,7,8,9,10,11,12,13,14,15),limits=c(1.5,15.5))+
  geom_hline(yintercept = 0, 
             linetype = 2, color = "black")+ ylab("ITT of Same-Election Eligibles") + xlab("")+
  theme_base1 + ggtitle("Without 1978") 
d1978

d1982<-ggplot(data1982, aes(x=id3, y =estimate)) +   
  geom_point(size = 1) +
  geom_errorbar(aes(ymax = max95, ymin = min95),width=0,  size=.4)+
  geom_errorbar(aes(ymax = max90, ymin = min90),width=0, size=.9)+
  scale_y_continuous(limits = c(-.1, .16))+ 
  scale_x_continuous(name="Election Counter",breaks = c(2,3,4,5,6,7,8,9,10,11,12,13,14,15),limits=c(1.5,15.5))+
  geom_hline(yintercept = 0, 
             linetype = 2, color = "black")+ ylab("ITT of Same-Election Eligibles") + xlab("")+
  theme_base1 + ggtitle("Without 1982") 
d1982

d1986<-ggplot(data1986, aes(x=id3, y =estimate)) +   
  geom_point(size = 1) +
  geom_errorbar(aes(ymax = max95, ymin = min95),width=0,  size=.4)+
  geom_errorbar(aes(ymax = max90, ymin = min90),width=0, size=.9)+
  scale_y_continuous(limits = c(-.1, .16))+ 
  scale_x_continuous(name="Election Counter",breaks = c(2,3,4,5,6,7,8,9,10,11,12,13,14,15),limits=c(1.5,15.5))+
  geom_hline(yintercept = 0, 
             linetype = 2, color = "black")+ ylab("ITT of Same-Election Eligibles") + xlab("")+
  theme_base1 + ggtitle("Without 1986") 
d1986

d1990<-ggplot(data1990, aes(x=id3, y =estimate)) +   
  geom_point(size = 1) +
  geom_errorbar(aes(ymax = max95, ymin = min95),width=0,  size=.4)+
  geom_errorbar(aes(ymax = max90, ymin = min90),width=0, size=.9)+
  scale_y_continuous(limits = c(-.1, .16))+ 
  scale_x_continuous(name="Election Counter",breaks = c(2,3,4,5,6,7,8,9,10,11,12,13,14,15),limits=c(1.5,15.5))+
  geom_hline(yintercept = 0, 
             linetype = 2, color = "black")+ ylab("ITT of Same-Election Eligibles") + xlab("")+
  theme_base1 + ggtitle("Without 1990") 
d1990

d1994<-ggplot(data1994, aes(x=id3, y =estimate)) +   
  geom_point(size = 1) +
  geom_errorbar(aes(ymax = max95, ymin = min95),width=0,  size=.4)+
  geom_errorbar(aes(ymax = max90, ymin = min90),width=0, size=.9)+
  scale_y_continuous(limits = c(-.1, .16))+ 
  scale_x_continuous(name="Election Counter",breaks = c(2,3,4,5,6,7,8,9,10,11,12,13,14,15),limits=c(1.5,15.5))+
  geom_hline(yintercept = 0, 
             linetype = 2, color = "black")+ ylab("ITT of Same-Election Eligibles") + xlab("")+
  theme_base1 + ggtitle("Without 1994") 
d1994

d1998<-ggplot(data1998, aes(x=id3, y =estimate)) +   
  geom_point(size = 1) +
  geom_errorbar(aes(ymax = max95, ymin = min95),width=0,  size=.4)+
  geom_errorbar(aes(ymax = max90, ymin = min90),width=0, size=.9)+
  scale_y_continuous(limits = c(-.1, .16))+ 
  scale_x_continuous(name="Election Counter",breaks = c(2,3,4,5,6,7,8,9,10,11,12,13,14,15),limits=c(1.5,15.5))+
  geom_hline(yintercept = 0, 
             linetype = 2, color = "black")+ ylab("ITT of Same-Election Eligibles") + xlab("")+
  theme_base1 + ggtitle("Without 1998") 
d1998

d2002<-ggplot(data2002, aes(x=id3, y =estimate)) +   
  geom_point(size = 1) +
  geom_errorbar(aes(ymax = max95, ymin = min95),width=0,  size=.4)+
  geom_errorbar(aes(ymax = max90, ymin = min90),width=0, size=.9)+
  scale_y_continuous(limits = c(-.1, .16))+ 
  scale_x_continuous(name="Election Counter",breaks = c(2,3,4,5,6,7,8,9,10,11,12,13,14,15),limits=c(1.5,15.5))+
  geom_hline(yintercept = 0, 
             linetype = 2, color = "black")+ ylab("ITT of Same-Election Eligibles") + xlab("")+
  theme_base1 + ggtitle("Without 2002") 
d2002

d2006<-ggplot(data2006, aes(x=id3, y =estimate)) +   
  geom_point(size = 1) +
  geom_errorbar(aes(ymax = max95, ymin = min95),width=0,  size=.4)+
  geom_errorbar(aes(ymax = max90, ymin = min90),width=0, size=.9)+
  scale_y_continuous(limits = c(-.1, .16))+ 
  scale_x_continuous(name="Election Counter",breaks = c(2,3,4,5,6,7,8,9,10,11,12,13,14,15),limits=c(1.5,15.5))+
  geom_hline(yintercept = 0, 
             linetype = 2, color = "black")+ ylab("ITT of Same-Election Eligibles") + xlab("")+
  theme_base1 + ggtitle("Without 2006") 
d2006

d2018<-ggplot(data2018, aes(x=id3, y =estimate)) +   
  geom_point(size = 1) +
  geom_errorbar(aes(ymax = max95, ymin = min95),width=0,  size=.4)+
  geom_errorbar(aes(ymax = max90, ymin = min90),width=0, size=.9)+
  scale_y_continuous(limits = c(-.1, .16))+ 
  scale_x_continuous(name="Election Counter",breaks = c(2,3,4,5,6,7,8,9,10,11,12,13,14,15),limits=c(1.5,15.5))+
  geom_hline(yintercept = 0, 
             linetype = 2, color = "black")+ ylab("ITT of Same-Election Eligibles") + xlab("")+
  theme_base1 + ggtitle("Without 2018") 
d2018


pdf("Graphs/MidtermElectionout2.pdf")
grid.arrange(d1958,d1962,d1966,d1970,d1974,d1978,d1982,d1986,
             d1990,d1994,d1998,d2002,d2006,d2018,respect=TRUE, ncol=4, nrow=4)
dev.off() 


